package com.china08.yxyapi.api.home.kecheng.sync;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSynchroCatalog;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSynchroGrade;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSynchroCatalogRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSynchroGradeRepository;

/**
 * 同步课程--列表页--通过学段、学科、版本筛选 分类
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class KechengSyncPhaceSubjectVersionListController extends YxyApiBaseController {

    @Autowired
    private YxyLessonsSynchroCatalogRepository yxyLessonsSynchroCatalogRepository;

    /** 分类 */
    @Autowired
    private YxyLessonsSynchroGradeRepository yxyLessonsSynchroGradeRepository;

    @RequestMapping(value = "/api_yw/home/kecheng/sync/phace/subject/version/lst/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel model) {

        String stageId = model.stageId;
        String subjectId = model.subjectId;
        String versionId = model.versionId;

        List<RespModel> returnLst = new ArrayList<>();
        RespModel aa = new RespModel();
        aa.gradeId = "";
        aa.gradeName = "不限";
        returnLst.add(aa);

        if (StringUtils.isNotBlank(versionId) && StringUtils.isNotBlank(subjectId) && StringUtils.isNotBlank(stageId)) {
            // 分类
            List<YxyLessonsSynchroCatalog> gradeLst = yxyLessonsSynchroCatalogRepository.findByGradeIdAll3(stageId,
                    subjectId, versionId);

            for (YxyLessonsSynchroCatalog yxyLessonsSynchroCatalog : gradeLst) {
                RespModel model1 = new RespModel();
                model1.gradeId = yxyLessonsSynchroCatalog.getGradeId();
                YxyLessonsSynchroGrade yxyLessonsSynchroGrades = yxyLessonsSynchroGradeRepository
                        .findById(model1.gradeId);
                model1.gradeName = yxyLessonsSynchroGrades.getName();
                returnLst.add(model1);
            }
        }
        if (StringUtils.isNotBlank(versionId) && !StringUtils.isNotBlank(subjectId)) {
            // 分类
            List<YxyLessonsSynchroCatalog> gradeLst = yxyLessonsSynchroCatalogRepository
                    .findByStageIdAndVersionAll(stageId, versionId);

            for (YxyLessonsSynchroCatalog yxyLessonsSynchroCatalog : gradeLst) {
                RespModel model1 = new RespModel();
                model1.gradeId = yxyLessonsSynchroCatalog.getGradeId();
                YxyLessonsSynchroGrade yxyLessonsSynchroGrades = yxyLessonsSynchroGradeRepository
                        .findById(model1.gradeId);
                model1.gradeName = yxyLessonsSynchroGrades.getName();
                returnLst.add(model1);
            }
        }
        if (!StringUtils.isNotBlank(versionId) && !StringUtils.isNotBlank(subjectId)) {
            // 分类
            List<YxyLessonsSynchroCatalog> gradeLst = yxyLessonsSynchroCatalogRepository.findByGradeIdAll1(stageId);

            for (YxyLessonsSynchroCatalog yxyLessonsSynchroCatalog : gradeLst) {
                RespModel model1 = new RespModel();
                model1.gradeId = yxyLessonsSynchroCatalog.getGradeId();
                YxyLessonsSynchroGrade yxyLessonsSynchroGrades = yxyLessonsSynchroGradeRepository
                        .findById(model1.gradeId);
                if (yxyLessonsSynchroGrades != null) {
                    model1.gradeName = yxyLessonsSynchroGrades.getName();
                }

                returnLst.add(model1);
            }
        }
        if (StringUtils.isNotBlank(stageId) && StringUtils.isNotBlank(subjectId)
                && !StringUtils.isNotBlank(versionId)) {
            // 分类
            List<YxyLessonsSynchroCatalog> gradeLst = yxyLessonsSynchroCatalogRepository
                    .findByStageIdAndSubjectAll(stageId, subjectId);

            for (YxyLessonsSynchroCatalog yxyLessonsSynchroCatalog : gradeLst) {
                RespModel model1 = new RespModel();
                model1.gradeId = yxyLessonsSynchroCatalog.getGradeId();
                YxyLessonsSynchroGrade yxyLessonsSynchroGrades = yxyLessonsSynchroGradeRepository
                        .findById(model1.gradeId);
                model1.gradeName = yxyLessonsSynchroGrades.getName();
                returnLst.add(model1);
            }
        }

        return returnLst;
    }

    /* 请求参数 */
    protected static class ReqModel extends YxyApiReqModel {

        // 学段id
        public String stageId;

        // 学科ID
        public String subjectId;

        // 版本ID
        public String versionId;
    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {

        // 分类ID
        public String gradeId;
        // 分类名称
        public String gradeName;
    }

}
